//导入函数
import {createRouter,createWebHistory} from 'vue-router';
import { ElMessage } from 'element-plus'
import { compile } from 'vue';

//准备数据
const routes=[
    {
        path:'/',
        redirect: "login"//重定向
    },    
    {
        //登录界面
        path:"/login",
        name:"login",
        component:()=>import("../components/Login.vue")
    },
    {
        //主界面
        path:"/main",
        name:"main",
        component:()=>import("../components/Main.vue"),
        redirect: "/main/dashboard",//默认首页
        children:[
            {
                //个人资料
                path:"userinfo",
                name:"userinfo",
                component:()=>import("../components/UserInfo.vue")
            },
            {
                //编号规则
                path:"coderule",
                name:"coderule",
                component:()=>import("../components/CodeRule.vue")
            },
            {
                //业务类型
                path:"businesstype",
                name:"businesstype",
                component:()=>import("../components/BusinessType.vue")
            },
            {
                //字典类型
                path:"dicttype",
                name:"dicttype",
                component:()=>import("../components/DictType.vue")
            },
            {
                //字典数据
                path:"dictdata/:dicttypeid",
                name:"dictdata",
                component:()=>import("../components/DictData.vue")
            },
            {
                //仓库表
                path:"warehouse",
                name:"warehouse",
                component:()=>import("../components/Warehouse.vue")
            },
            {
                //仓库添加页面
                path: 'warehouseadd',
                name: 'WarehouseAdd',
                component: () => import('../components/WarehouseAdd.vue'),
                meta: { title: '添加仓库' }
            },
            {
                //仓库详情页面
                path: 'WarehouseDetail',
                name: 'WarehouseDetail',
                component: () => import('../components/WarehouseDetail.vue'),
                meta: { title: '仓库详情' },
            },
            {
                //仓库修改页面
               path: 'warehouseupdate',
               name: 'WarehouseUpdate',
               component: () => import('../components/WarehouseUpdate.vue'),
               meta: { title: '编辑仓库' }
            },
            {
                //库存查询
                path:"SelectWarehouse",
                name:"SelectWarehouse",
                component:()=>import("../components/SelectWarehouse.vue"),
                meta:{title:"库存查询"}
            },
            {
                //保质期查询
                path:"SelectShelflife",
                name:"SelectShelflife",
                component:()=>import("../components/SelectShelflife.vue"),
                meta:{title:"保质期查询"}
            },
            {
                //供应商管理
                path:"supplier",
                name:"supplier",
                component:()=>import("../components/Supplier.vue")
            },
            {
                //客户管理
                path:"customer",
                name:"customer",
                component:()=>import("../components/Customer.vue")
            },
            {
                //供应商新增
                path:"suppliernew",
                name:"suppliernew",
                component:()=>import("../components/SupplierNew.vue")
            },
            {
                //供应商详情
                path:"supplierdetail",
                name:"supplierdetail",
                component:()=>import("../components/SupplierDetail.vue")
            },
            {
                //客户新增
                path:"customernew",
                name:"customernew",
                component:()=>import("../components/CustomerNew.vue")
            },
            {
                //供应商详情
                path:"customerdetail",
                name:"customerdetail",
                component:()=>import("../components/CustomerDetail.vue")
            },
            {
                //货品类型
                path:"producttype",
                name:"producttype",
                component:()=>import("../components/ProductType.vue")
            },
            {
                //货品信息
                path:"product",
                name:"product",
                component:()=>import("../components/Product.vue")
            },
            {
                //货品信息添加页面
                path:"productadd",
                name:"productadd",
                component:()=>import("../components/ProductAdd.vue")
            },
            {
                //部门信息页面
                path:"department",
                name:"department",
                component:()=>import("../components/Department.vue")
            },
            {
                //货品信息修改页面
                path:"productupdate/:productid/:productcode",
                name:"productupdate",
                component:()=>import("../components/ProductUpdate.vue")
            },
            {
                //货品信息详情页面
                path:"productInfo/:productid/:productcode",
                name:"productInfo",
                component:()=>import("../components/ProductInfo.vue")
            },
            {
                //进货订单页面
                path:"purchase",
                name:"purchase",
                component:()=>import("../components/Purchase.vue")
            },
            {
                //新增进货订单页面
                path:"purchaseadd",
                name:"purchaseadd",
                component:()=>import("../components/PurchaseAdd.vue")
            },
            {
                //修改进货订单页面
                path:"purchaseupdate/:purid/:purcode",
                name:"purchaseupdate",
                component:()=>import("../components/PurchaseUpdate.vue")
            },
            {
                //进货订单详情页面
                path:"purchaseinfo/:purid/:purcode",
                name:"purchaseinfo",
                component:()=>import("../components/PurchaseInfo.vue")
            },
            {
                //销售订单页面
                path:"sale",
                name:"sale",
                component:()=>import("../components/Sale.vue")
            },
            {
                //新增销售订单页面
                path:"saleadd",
                name:"saleadd",
                component:()=>import("../components/SaleAdd.vue")
            },
            {
                //修改销售订单页面
                path:"saleupdate/:saleid/:salecode",
                name:"saleupdate",
                component:()=>import("../components/SaleUpdate.vue")
            },
            {
                //销售订单详情页面
                path:"saleinfo/:saleid/:salecode",
                name:"saleinfo",
                component:()=>import("../components/SaleInfo.vue")
            },
            {
                //销售统计页面
                path:"TotalSales",
                name:"TotalSales",
                component:()=>import("../components/TotalSales.vue")
            },
            {
                //进货统计页面
                path:"Totalpurchase",
                name:"Totalpurchase",
                component:()=>import("../components/Totalpurchase.vue")
            },
            {
                //入库流水
                path:"InWater",
                name:"InWater",
                component:()=>import("../components/InWater.vue")
            },
            {
                //出库流水
                path:"OutWater",
                name:"OutWater",
                component:()=>import("../components/OutWater.vue")
            },
            {
                //库存分布
                path:"Distribution",
                name:"Distribution",
                component:()=>import("../components/Distribution.vue")
            },
            {
                //库存汇总
                path:"WarehouseRollup",
                name:"WarehouseRollup",
                component:()=>import("../components/WarehouseRollup.vue")
            },
            {
                //出入库类型汇总
                path:"OutInTypeRollup",
                name:"OutInTypeRollup",
                component:()=>import("../components/OutInTypeRollup.vue")
            },
            {
                //岗位管理页面
                path:"position",
                name:"position",
                component:()=>import("../components/Position.vue")
            },
            {
                //用户管理页面
                path:"user",
                name:"user",
                component:()=>import("../components/User.vue")
            },
            {
                //入库管理页面
                path:"instock",
                name:"instock",
                component:()=>import("../components/Instock.vue")
            },
            {
                //入库新增页面
                path:"instockadd",
                name:"instockadd",
                component:()=>import("../components/InstockAdd.vue")
            },
            {
                //入库新增页面
                path:"role",
                name:"role",
                component:()=>import("../components/Role.vue")
            },
            {
                //入库修改页面
                path:"instockupdate/:instockid/:instockcode",
                name:"instockupdate",
                component:()=>import("../components/InstockUpdate.vue")
            },
            {
                //入库详情
                path:"instockinfo/:instockid/:instockcode",
                name:"instockinfo",
                component:()=>import("../components/InstockInfo.vue")
            },
            {
                //出库管理页面
                path:"outstock",
                name:"outstock",
                component:()=>import("../components/Outstock.vue")
            },
            {
                //出库新增页面
                path:"outstockadd",
                name:"outstockadd",
                component:()=>import("../components/OutstockAdd.vue")
            },
            {
                //出库修改页面
                path:"outstockupdate/:outstockid/:outstockcode",
                name:"outstockupdate",
                component:()=>import("../components/OutstockUpdate.vue")
            },
            {
                //出库详情
                path:"outstockinfo/:outstockid/:outstockcode",
                name:"outstockinfo",
                component:()=>import("../components/OutstockInfo.vue")
            },
            {
                //进货退货页面
                path:"purchasereturn",
                name:"purchasereturn",
                component:()=>import("../components/PurchaseReturn.vue")
            },
            {
                //进货退货新增页面
                path:"purchasereturnadd",
                name:"purchasereturnadd",
                component:()=>import("../components/PurchaseReturnAdd.vue")
            },
            {
                //进货退货修改页面
                path:"purchasereturnupdate/:purtid/:purtcode",
                name:"purchasereturnupdate",
                component:()=>import("../components/PurchaseReturnUpdate.vue")
            },
            {
                //进货退货详情页面
                path:"purchasereturninfo/:purtid/:purtcode",
                name:"purchasereturninfo",
                component:()=>import("../components/PurchaseReturnInfo.vue")
            },
            {
                //库存调拨页面
                path:"allot",
                name:"allot",
                component:()=>import("../components/Allocation.vue")
            },
            {
                //库存调拨添加页面
                path:"allotadd",
                name:"allotadd",
                component:()=>import("../components/AllocationAdd.vue")
            },
            {
                //库存调拨修改页面
                path:"allotupdate",
                name:"allotupdate",
                component:()=>import("../components/AllocationUpdate.vue")
            },
            {
                //库存调拨详细页面
                path:"allocationdetail",
                name:"allocationdetail",
                component:()=>import("../components/AllocationDetail.vue")
            },
            {
                //调拨统计页面
                path:"TotalAllocation",
                name:"TotalAllocation",
                component:()=>import("../components/TotalAllocation.vue")
            },
            {
                //销售退货页面
                path:"salereturn",
                name:"salereturn",
                component:()=>import("../components/SaleReturn.vue")
            },
            {
                //销售退货新增页面
                path:"salereturnadd",
                name:"salereturnadd",
                component:()=>import("../components/SaleReturnAdd.vue")
            },
            {
                //销售退货修改页面
                path:"salereturnupdate/:saletid/:saletcode",
                name:"salereturnupdate",
                component:()=>import("../components/SaleReturnUpdate.vue")
            },
            {
                //销售退货详情页面
                path:"salereturninfo/:saletid/:saletcode",
                name:"salereturninfo",
                component:()=>import("../components/SaleReturnInfo.vue")
            },
            {
                //库存预警页面
                path:"stockwarning",
                name:"stockwarning",
                component:()=>import("../components/StockWarning.vue")
            },
            {
                //信息公告页面
                path:"info",
                name:"info",
                component:()=>import("../components/Info.vue")
            },
            {
                //首页  
                path:"dashboard",
                name:"dashboard",
                component:()=>import("../components/Dashboard.vue")
            },
            {
                //盘点页面
                path:"check",
                name:"check",
                component:()=>import("../components/Check.vue")
            },
            {
                //盘点新增页面
                path:"checkadd",
                name:"checkadd",
                component:()=>import("../components/Checkadd.vue")
            },
            {
                //盘点修改页面
                path:"checkupdate/:checkid/:checkcode",
                name:"checkupdate",
                component:()=>import("../components/CheckUpdate.vue")
            },
            {
                //盘点详情页面
                path:"checkinfo/:checkid/:checkcode",
                name:"checkinfo",
                component:()=>import("../components/CheckInfo.vue")
            },
            {
                //盘点统计页面
                path:"TotalCheck",
                name:"TotalCheck",
                component:()=>import("../components/TotalCheck.vue")
            }
        ]
    }
]

//创建路由
const router=createRouter({
    history:createWebHistory(),//模式
    routes:routes//数据
})

// 路由守卫
router.beforeEach((to, from, next) => {
    // 检查目标路径是否是登录页
    if (to.path !== "/login") {
        // 检查 token 是否存在
        const token = sessionStorage.getItem('token');
        if (token==null) {
            ElMessage.error('请登录');
            next('/login'); // 跳转到登录页
        } else {
            next(); // 放行
        }
    } else {
        next(); // 如果是登录页，直接放行
    }
});

//导出
export default router;